/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ga.coordination;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Cursor;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;

/**
 *
 * @author user
 */
public class GAAnim extends JPanel{   
    //Label
    private JLabel jLP;
    private JLabel jLS;
    private JLabel jLC;
    private JLabel jLM;
    private JLabel H;
    
    //TextField
    private JTextField TFP;
    private JTextField TFS;
    private JTextField TFC;
    private JTextField TFM;
    
    //Panel
    private JPanel P;
    private JPanel S;
    private JPanel C;
    private JPanel M;
    private JPanel All;
    
    //Button
    private JButton Start;
          
    //ImageIcon
    private ImageIcon population;
    private ImageIcon selection;
    private ImageIcon crossover;
    private ImageIcon mutation;
    
    ArrayList<Byte> po = new ArrayList();
    ArrayList<Byte> se = new ArrayList();
    ArrayList<Byte> co = new ArrayList();
    ArrayList<Byte> mu = new ArrayList();
    
    int x = 1;
    
    public GAAnim() throws InterruptedException{  
        po.add(((byte)1)); 
        po.add(((byte)2)); 
        po.add(((byte)3)); 
        po.add(((byte)4));  
        se.add(((byte)1)); 
        se.add(((byte)2)); 
        se.add(((byte)3)); 
        se.add(((byte)4));  
        co.add(((byte)1)); 
        co.add(((byte)2)); 
        co.add(((byte)3)); 
        co.add(((byte)4));  
        mu.add(((byte)1)); 
        mu.add(((byte)2)); 
        mu.add(((byte)3)); 
        mu.add(((byte)4));
        
        population = new ImageIcon("src/asset/population.png");
        selection = new ImageIcon("src/asset/choosing.jpg");
        crossover = new ImageIcon("src/asset/crossover.jpg");
        mutation = new ImageIcon("src/asset/mutation.jpg");
        
        jLP = new JLabel("Population");
        jLS = new JLabel("Selection");
        jLC = new JLabel("CrossOver");
        jLM = new JLabel("Mutation");
        H = new JLabel();
        
        Start = new JButton("Start NOW");
        Start.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
        Start.addActionListener(new ActionListener(){
            @Override
            public void actionPerformed(ActionEvent e)
            {
                try {
                    Printa(e);
                } catch (InterruptedException ex) {
                    Logger.getLogger(GAAnim.class.getName()).log(Level.SEVERE, null, ex);
                }
            }

            private void Printa(ActionEvent e) throws InterruptedException {
                print(po,se,co,mu);
            }
        });
        
        TFP = new JTextField();
        TFP.setEditable(false);
        TFS = new JTextField();
        TFS.setEditable(false);
        TFC = new JTextField();
        TFC.setEditable(false);
        TFM = new JTextField(); 
        TFM.setEditable(false);       
                
        H.setIcon(null);
        
        P = new JPanel();
        P.setLayout(new GridLayout(2,1));
        P.add(jLP);
        P.add(TFP);
        
        S = new JPanel();
        S.setLayout(new GridLayout(2,1));
        S.add(jLS);
        S.add(TFS);
        
        C = new JPanel();
        C.setLayout(new GridLayout(2,1));
        C.add(jLC);
        C.add(TFC);
        
        M = new JPanel();
        M.setLayout(new GridLayout(2,1));
        M.add(jLM);
        M.add(TFM);
        
        All = new JPanel();
        All.setLayout(new GridLayout(1,4));
        All.add(P);
        All.add(S);
        All.add(C);
        All.add(M); 
        
        this.setLayout(new GridLayout(3,1));
        this.add(H);
        this.add(All);
        this.add(Start);
    }
    
    public void print(ArrayList<Byte> po, ArrayList<Byte> se, ArrayList<Byte> co, ArrayList<Byte> mu) throws InterruptedException{
        for(int j = 0; j < 4; j++){
            for(int i = 0; i < 4; i++){
                switch(i){
                    case 0: {
                                H.setIcon(population);
                                TFP.setText((po.get(j)).toString());
                                jLP.setForeground(Color.BLUE);
                                jLS.setForeground(Color.black);
                                jLC.setForeground(Color.black);
                                jLM.setForeground(Color.black);
                                revalidate();
                            }
                            break;
                    case 1: {
                                H.setIcon(selection);
                                TFS.setText((se.get(j)).toString());
                                jLP.setForeground(Color.BLACK);
                                jLS.setForeground(Color.BLUE);
                                jLC.setForeground(Color.black);
                                jLM.setForeground(Color.black);
                                revalidate();
                            }
                            break;
                    case 2: {
                                H.setIcon(crossover);
                                TFC.setText((co.get(j)).toString());
                                jLP.setForeground(Color.BLACK);
                                jLS.setForeground(Color.BLACK);
                                jLC.setForeground(Color.BLUE);
                                jLM.setForeground(Color.black);
                                revalidate();
                            }
                            break;
                    case 3: {
                                H.setIcon(mutation);
                                TFM.setText((mu.get(j)).toString());
                                jLP.setForeground(Color.BLACK);
                                jLS.setForeground(Color.BLACK);
                                jLC.setForeground(Color.black);
                                jLM.setForeground(Color.BLUE);
                                revalidate();
                            }
                            break;
                }
                for(int k = 0; k < 50; k++){
                    Thread.sleep(1);
                }
                this.revalidate();
            }
        }        
    }

    public static void main(String[] args) throws InterruptedException {
        new GAAnim();
    }
}